package com.todo.ui.presenters
{
	import com.todo.events.TaskEvent;
	import com.todo.model.vos.Task;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	
	public class TaskDetailPresentationModel extends EventDispatcher
	{
		// -------------------------------------------------------
		// Setters and getters
		// -------------------------------------------------------
		
		// selectedTask ...................................................
		private var tempTask:Task;
		private var _selectedTask:Task = null;
		
		[Bindable(Event="taskChange")]
		public function set selectedTask( task:Task ):void
		{
			_selectedTask = task;
			if( task )
			{
				tempTask = new Task();
				tempTask.copyFrom( task );
			}
			dispatchEvent( new Event("taskChange") );
		}
		public function get selectedTask( ):Task
		{
			return _selectedTask;
		}
		
		//  descriptionErrorString ...................................................
		private var _descriptionErrorString:String = "";
		
		[Bindable(Event="validationChange")]
		public function get descriptionErrorString():String
		{
			return _descriptionErrorString
		}
		
		//  TypeErrorString ...................................................
		private var _typeErrorString:String = "";
		
		[Bindable(Event="validationChange")]
		public function get typeErrorString():String
		{
			return _typeErrorString;
		}
		
		
		// -------------------------------------------------------
		// Contructor
		// -------------------------------------------------------
		
		private var dispatcher:IEventDispatcher
		public function TaskDetailPresentationModel( dispatcher:IEventDispatcher)
		{
			this.dispatcher = dispatcher;
		}
		
		
		// -------------------------------------------------------
		// Public methods
		// -------------------------------------------------------
		
		// cancelTaskEdits ...................................................
		public function cancelTaskEdits() : void 
		{
			var event:TaskEvent = new TaskEvent(TaskEvent.CANCEL_EDIT);
			event.task = selectedTask;
			
			dispatcher.dispatchEvent(event);
		}
		
		
		// deleteTask ...................................................
		public function deleteTask( ) : void 
		{
			// broadcast the event
			var event:TaskEvent = new TaskEvent(TaskEvent.DELETE);
			event.task =  selectedTask;
			
			dispatcher.dispatchEvent(event);
		}
		
		// saveTask ...................................................
		public function saveTask( ):void
		{
			if( isValid( tempTask ) )
			{
				// to make it here the fields must have been valid
				// broadcast the event
				var event:TaskEvent = new TaskEvent(TaskEvent.SAVE);
				event.task = tempTask;
				
				dispatcher.dispatchEvent(event);
			}
		}
		
		public function updateDescription( description:String ):void
		{
			tempTask.description = description;
		}
		
		public function updateType( type:String ):void
		{
			tempTask.type = type;
		}
		
		
		// -------------------------------------------------------
		// Private methods
		// -------------------------------------------------------
		
		//  isValid ...................................................
		private function isValid( task:Task ):Boolean
		{
			_descriptionErrorString = ( task.description ) ? "" : "Description is a required field.";
			_typeErrorString  = ( task.type  ) ? "" : "Type is a required field.";
			dispatchEvent( new Event( "validationChange" ) );
			return ( task.description  &&  task.type );
		}
	}
}